#include <cstdio>
#include <cstring>
struct nod{
    char id[16];
    int h,m,s;
}tmp,res1,res2;//tmp用于读取数据，res1用于记录当前最早到达房间的人的id以及时间
bool greater(nod op1,nod op2)
{//用于比较时间
    if(op1.h!=op2.h)return op1.h>op2.h;
    else if(op1.m!=op2.m)return op1.m>op2.m;
    else return op1.s>op2.s;
}
int main()
{
    res1.h=24;res1.m=60;res1.s=60;
    res2.h=0;res2.m=0;res2.s=0;//初始化
    int num;
    scanf("%d",&num);
    while(num--)
    {
        scanf("%s%d:%d:%d",tmp.id,&tmp.h,&tmp.m,&tmp.s);
        if(greater(res1,tmp))
        {
            strcpy(res1.id,tmp.id);
            res1.h=tmp.h;res1.m=tmp.m;res1.s=tmp.s;
        }
        scanf("%d:%d:%d",&tmp.h,&tmp.m,&tmp.s);
        if(greater(tmp,res2))
        {
            strcpy(res2.id,tmp.id);
            res2.h=tmp.h;res2.m=tmp.m;res2.s=tmp.s;
        }
    }
    printf("%s %s",res1.id,res2.id);
    return 0;
}
